package dialogs;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import com.globapp.findandkeep.R;

/**
 * Fragmento para mostrar el dialogo con las listas posibles scaneadas
 * 
 * @author Marco
 * 
 */
public class ScanDialog extends DialogFragment {
	/* Interface para comunicar con la activiy que habre el dialogo */
	public interface NoticeDialogListener {
		public void onSSIDSelected(String ssidSelecionada);
	}

	// Use this instance of the interface to deliver action events
	NoticeDialogListener mListener;

	// Override the Fragment.onAttach() method to instantiate the
	// NoticeDialogListener
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		// Verify that the host activity implements the callback interface
		try {
			// Instantiate the NoticeDialogListener so we can send events to the
			// host
			mListener = (NoticeDialogListener) activity;
		} catch (ClassCastException e) {
			// The activity doesn't implement the interface, throw exception
			throw new ClassCastException(activity.toString()
					+ " must implement NoticeDialogListener");
		}
	}

	public String[] listaSSID;

	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		if (savedInstanceState != null) {
			listaSSID = savedInstanceState.getStringArray("dispositivos");
		}
		// Use the Builder class for convenient dialog construction
		AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
		builder.setTitle(R.string.s_dialog_titulo_ssid_disponibles);
		if (listaSSID != null && listaSSID.length > 0) {
			// si se cumple la condicion, quiere decir que se encontraron redes
			builder.setItems(listaSSID, new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
					mListener.onSSIDSelected(listaSSID[which]);
				}
			});
		} else {
			// no se encontraron redes, armo un dialogo diferente
			builder.setMessage(R.string.s_dialog_sin_redes_disponibles);
			builder.setPositiveButton("OK",
					new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int id) {
							dismiss();
						}
					});
		}
		// Create the AlertDialog object and return it
		return builder.create();
	}

	public void onSaveInstanceState(Bundle savedInstanceState) {
		super.onSaveInstanceState(savedInstanceState);
		savedInstanceState.putStringArray("dispositivos", listaSSID);

	}
}
